﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

public class PlayerController : MonoBehaviour
{

    private Rigidbody2D rb;
    private Animator anim;

    private Vector2 moveVelocity;
    public float speed;
    public float jumpforce;
    public Stand _Playerstand;
    private bool CanShowChangeGravaty = true;
    private SpriteRenderer sr;
    private void Start()
    {

        //_Playerstand = Stand.up;
        //_Playerstand = Stand.up;
        anim = GetComponent<Animator>();
        anim.SetBool("aaa", true);
        rb = GetComponent<Rigidbody2D>();
        sr = GetComponent<SpriteRenderer>();
    }

    private void Update()
    {
        float movedir = Input.GetAxisRaw("Horizontal");
        Vector2 moveInput = transform.right *movedir;
        moveVelocity = moveInput * speed;
        if (moveInput != Vector2.zero)
        {
            if(movedir>0)
            {
                sr.flipX = false;
            }
            else
            {
                sr.flipX = true;
            }
            anim.SetBool("zuo", true);
        }
        else if (moveInput == Vector2.zero)
        {
            anim.SetBool("zuo", false);
            anim.SetBool("you", false);
        }

        if (Input.GetKeyDown(KeyCode.Q) && CanShowChangeGravaty)
        {
            CanShowChangeGravaty = false;
            ZYKTool.Timer.ZYKTimerSystemTool.Instance.ZYKTimerAddTimerTask
                (() => { CanShowChangeGravaty = true; }, 2, 1, ZYKTool.Timer.PETimeUnit.Second);
            改变重力();
            Debug.Log("！");
        }
        anim.SetBool("stay", false);
        ZYKTool.Timer.ZYKTimerSystemTool.Instance.ZYKTimerAddTimerTask
                (() => anim.SetBool("stay", true), 2, 1, ZYKTool.Timer.PETimeUnit.Second);
    }
    private void FixedUpdate()
    {
        rb.MovePosition(rb.position + moveVelocity * Time.fixedDeltaTime);
    }
    public void 玩家当前站立位置(Stand stand)
    {
        _Playerstand = stand;
        if (CanShowChangeGravaty == false)
        {
            Debug.LogError("停止改变重力");
            anim.SetBool("switch", false);
            transform.DOKill();

        }
        else
        {
            anim.SetBool("aaa", true);
            ZYKTool.Timer.ZYKTimerSystemTool.Instance.ZYKTimerAddTimerTask
     (() => { anim.SetBool("aaa", false); }, 0.2f, 1, ZYKTool.Timer.PETimeUnit.Second);
        }
        /*
        switch (_Playerstand)
        {
            case Stand.down:
                Debug.LogError("??");
                transform.DORotate(new Vector3(0, 0, 180), 1, RotateMode.Fast);
                _Playerstand = Stand.up;
                break;
            case Stand.up:
                Debug.LogError("！？");
                transform.DORotate(new Vector3(0, 0, 0), 1, RotateMode.Fast);
                _Playerstand = Stand.down;
                break;
        }
        */



        //todo:黄：停止改变重力
        //transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z);
        //transform.Rotate(180, 0, 0);

    }
    public void 改变重力()
    {
        //todo 黄：改变重力
        anim.SetBool("switch", true);
        transform.DOKill(); Debug.LogError(_Playerstand);
        switch (_Playerstand)
        {
            case Stand.down:
                Debug.LogError("起飞");
                // transform.DORotate(new Vector3(0, 0, 180), 1, RotateMode.Fast);
                transform.DOMove(new Vector3(transform.position.x, 40 + transform.position.y, transform.position.z), 1);
                break;
            case Stand.up:
                Debug.LogError("下坠");
                transform.DOMove(new Vector3(transform.position.x, -40 + transform.position.y, transform.position.z), 1);
                break;
            case Stand.left:
                Debug.LogError("右冲");
                transform.DOMove(new Vector3(92 + transform.position.x, transform.position.y, transform.position.z), 1);
                break;
            case Stand.right:
                Debug.LogError("左冲");
                transform.DOMove(new Vector3(-92 + transform.position.x, transform.position.y, transform.position.z), 1);
                break;
        }
    }
    public enum Stand
    {
        up,
        down,
        right,
        left,
    }
}